﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DayPilot.Web.Ui.Events;
using DayPilot.Web.Ui.Events.Bubble;
using System.Collections.Generic;
using System.Data.SqlClient;
using DayPilot.Web.Ui.Enums;

public partial class includes_ucPatientCalendar : System.Web.UI.UserControl
{
    private DataTable table;

    protected void Page_Load(object sender, EventArgs e)
    {



        List<CalendarEvent> CalendarEventList = new List<CalendarEvent>();
        List<DentistCalendar> dentistCalendarEventList = new List<DentistCalendar>();
        #region Data loading initialization
        if (Session["AllFeatures"] == null)
        {
            if (Request.QueryString["Id"] != null)
            {
                Session["dcotorId"] = Request.QueryString["Id"].ToString();


                CalendarEventList = CalenderRepository.GetCalendarEventsBYDrId(Convert.ToInt32(Session["dcotorId"]));
                DayPilotCalendar1.DataSource = CalendarEventList;
                DayPilotNavigator1.DataSource = CalendarEventList;
            }
            else if (Request.QueryString["dentistId"] !=null)
            {
                Session["dentistId"] = Request.QueryString["dentistId"].ToString();

                dentistCalendarEventList = CalenderRepository.GetDentistEventsBYDentistId(Convert.ToInt32(Session["dentistId"]));
                DayPilotCalendar1.DataSource = dentistCalendarEventList;
                DayPilotNavigator1.DataSource = dentistCalendarEventList;

            }


        }

        ////if (Session["PatientId"] != null)
        ////{
        ////    DayPilotMenu1.Controls.RemoveAt(2);
        ////    ////DayPilotMenu1.FindControl("Delete");
        ////}



        #endregion

        if (!IsPostBack)
        {
            DayPilotCalendar1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek();
            DataBind();
        }
    }

    protected void DayPilotCalendar1_EventMove(object sender, EventMoveEventArgs e)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            con.Open();
            if (Request.QueryString["Id"] != null)
            {
                SqlCommand cmd = new SqlCommand("UPDATE [CalendarEvent] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.Parameters.AddWithValue("start", e.NewStart);
                cmd.Parameters.AddWithValue("end", e.NewEnd);
                cmd.ExecuteNonQuery();
            }
            else
            {
                SqlCommand cmd = new SqlCommand("UPDATE [DentistCalendar] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.Parameters.AddWithValue("start", e.NewStart);
                cmd.Parameters.AddWithValue("end", e.NewEnd);
                cmd.ExecuteNonQuery();

            }

        }

        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update();

       
    }

    protected void DayPilotCalendar1_EventMenuClick(object sender, EventMenuClickEventArgs e)
    {
        if (e.Command == "Delete")
        {

            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                con.Open();
                if (Request.QueryString["Id"] != null)
                {
                    SqlCommand cmd = new SqlCommand("DELETE FROM [CalendarEvent] WHERE [id] = @id", con);
                    cmd.Parameters.AddWithValue("id", e.Value);
                    cmd.ExecuteNonQuery();
                    DayPilotCalendar1.DataBind();
                    DayPilotCalendar1.Update();
                }
                else
                {
                    SqlCommand cmd = new SqlCommand("DELETE FROM [DentistCalendar] WHERE [id] = @id", con);
                    cmd.Parameters.AddWithValue("id", e.Value);
                    cmd.ExecuteNonQuery();
                    DayPilotCalendar1.DataBind();
                    DayPilotCalendar1.Update();

                }

            }



        }
        else if (e.Command == "AppointmentDetail")
        {
            Session["eventId"] = e.Value;

            if (Request.QueryString["Id"] != null)
            {
               // Response.Redirect("~/Doctor/doctorAppointmentDetail.aspx");
                Response.Redirect("~/Doctor/NewDoctorAppointment.aspx");
            }
            else
            {

                Response.Redirect("~/Dentist/DentistAppointmentDetail.aspx");
            }

        }
    }
    protected void DayPilotCalendar1_TimeRangeSelected(object sender, TimeRangeSelectedEventArgs e)
    {
        //if (Request.QueryString["Id"] != null)
        //{
        DayPilotCalendar1.EventClickHandling = DayPilot.Web.Ui.Enums.EventClickHandlingEnum.Disabled;
        DayPilotCalendar1.EventDeleteHandling = DayPilot.Web.Ui.Enums.UserActionHandling.Disabled;
        DayPilotCalendar1.EventDoubleClickHandling = DayPilot.Web.Ui.Enums.EventClickHandlingEnum.Disabled;
        DayPilotCalendar1.TimeRangeSelectedHandling = DayPilot.Web.Ui.Enums.TimeRangeSelectedHandling.Disabled;
        //}


        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("New event created.");
    }

    protected void DayPilotCalendar1_EventResize(object sender, EventResizeEventArgs e)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            con.Open();

            if (Request.QueryString["Id"] != null)
            {
                SqlCommand cmd = new SqlCommand("UPDATE [CalendarEvent] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.Parameters.AddWithValue("start", e.NewStart);
                cmd.Parameters.AddWithValue("end", e.NewEnd);
                cmd.ExecuteNonQuery();
            }
            else
            {
                SqlCommand cmd = new SqlCommand("UPDATE [DentistCalendar] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.Parameters.AddWithValue("start", e.NewStart);
                cmd.Parameters.AddWithValue("end", e.NewEnd);
                cmd.ExecuteNonQuery();

            }

        }


        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("Event resized");
    }

    protected void DayPilotCalendar1_EventClick(object sender, EventClickEventArgs e)
    {
        Session["eventId"] = e.Value;
        if (Request.QueryString["Id"] != null)
        {
            Response.Redirect("doctorAppointmentDetail.aspx");
        }
        else
        {
            Response.Redirect("DentistAppointmentDetail.aspx");

        }
    }
    protected void DayPilotCalendar1_EventEdit(object sender, EventEditEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            dr["name"] = e.NewText;
            table.AcceptChanges();
        }

        #endregion

        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("Event text changed.");
    }
    protected void DayPilotCalendar1_BeforeEventRender(object sender, BeforeEventRenderEventArgs e)
    {
        if (e.Value == "6")
        {
            e.BorderColor = "#1AAFE0";
            e.BackgroundColor = "#90D8F2";
        }
        if (e.Value == "8")
        {
            e.BorderColor = "#068c14";
            e.BackgroundColor = "#08b81b";
        }
        if (e.Value == "2")
        {
            e.BorderColor = "#990607";
            e.BackgroundColor = "#f60e13";
        }
    }


    protected void DayPilotCalendar1_EventDelete(object sender, EventDeleteEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            table.Rows.Remove(dr);
            table.AcceptChanges();
        }

        #endregion

        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("Event deleted.");
    }

    protected void DayPilotCalendar1_EventSelect(object sender, DayPilotEventArgs e)
    {
        DayPilotCalendar1.Update();
    }

    protected void DayPilotCalendar1_Refresh(object sender, RefreshEventArgs e)
    {
        DayPilotCalendar1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek(e.StartDate);
        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update(CallBackUpdateType.Full);
    }


    protected void DayPilotCalendar1_BeforeHeaderRender(object sender, BeforeHeaderRenderEventArgs e)
    {
        if (e.IsCorner)
        {
            e.InnerHTML = String.Format("<div style='padding:2px; text-align:center; font-weight: bold;'>{0}</div>", DayPilotCalendar1.StartDate.Year);
        }
    }

    protected void DayPilotBubble1_RenderContent(object sender, RenderEventArgs e)
    {
        if (e is RenderEventBubbleEventArgs)
        {
            RenderEventBubbleEventArgs re = e as RenderEventBubbleEventArgs;
            re.InnerHTML = "<b>Event details</b><br />Here is the right place to show details about the event with ID: " + re.Value + ". This text is loaded dynamically from the server.<br/><b>Double-click the event to enter inline edit mode.</b>";
        }
        else if (e is RenderTimeBubbleEventArgs)
        {
            RenderTimeBubbleEventArgs re = e as RenderTimeBubbleEventArgs;
            e.InnerHTML = "<b>Time header details</b><br />From:" + re.Start + "<br />To: " + re.End;
        }
        else if (e is RenderCellBubbleEventArgs)
        {
            RenderCellBubbleEventArgs re = e as RenderCellBubbleEventArgs;
            e.InnerHTML = "<b>Cell details</b><br />Column:" + re.ResourceId + "<br />From:" + re.Start + "<br />To: " + re.End;
        }
    }

    protected void DayPilotCalendar1_TimeRangeDoubleClick(object sender, TimeRangeDoubleClickEventArgs e)
    {
        #region Simulation of database update
        DataRow dr = table.NewRow();
        dr["start"] = e.Start;
        dr["end"] = e.End;
        dr["id"] = Guid.NewGuid().ToString();
        dr["name"] = "New event";

        table.Rows.Add(dr);
        table.AcceptChanges();
        #endregion

        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("New event created.");

    }

    protected void DayPilotCalendar1_TimeRangeMenuClick(object sender, TimeRangeMenuClickEventArgs e)
    {
        if (e.Command == "Insert")
        {
            #region Simulation of database update

            DataRow dr = table.NewRow();
            dr["start"] = e.Start;
            dr["end"] = e.End;
            dr["id"] = Guid.NewGuid().ToString();
            dr["name"] = "New event";

            table.Rows.Add(dr);
            table.AcceptChanges();
            #endregion

            DayPilotCalendar1.DataBind();
            DayPilotCalendar1.Update();

        }

    }
    protected void DayPilotCalendar1_Command(object sender, DayPilot.Web.Ui.Events.CommandEventArgs e)
    {
        switch (e.Command)
        {
            case "previous":
                DayPilotCalendar1.StartDate = DayPilotCalendar1.StartDate.AddDays(-7);
                DayPilotCalendar1.DataBind();
                DayPilotCalendar1.Update(CallBackUpdateType.Full);
                break;
            case "navigate":
                DateTime start = (DateTime)e.Data["start"];
                DateTime end = (DateTime)e.Data["end"];

                DayPilotCalendar1.StartDate = start;
                //DayPilotCalendar1.Days = (int) (end - start).TotalDays;

                DayPilotCalendar1.DataBind();
                DayPilotCalendar1.Update(CallBackUpdateType.Full);
                break;
            case "refresh":
                DayPilotCalendar1.DataBind();
                DayPilotCalendar1.Update();
                break;
        }


    }
    protected void DayPilotNavigator1_VisibleRangeChanged(object sender, EventArgs e)
    {
        DayPilotNavigator1.DataBind();
    }
    protected void DayPilotCalendar1_BeforeCellRender(object sender, BeforeCellRenderEventArgs e)
    {
        if (e.Start >= DateTime.Today && e.End <= DateTime.Today.AddDays(1))
        {
            e.BackgroundColor = "#FFFFCE";
        }
    }
    protected void btnBackToSearch_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["Id"] != null)
        {
            Response.Redirect("~/dicountDoctorDentist/discountDoctors.aspx");
        }
        else
        
        {
            Response.Redirect("~/dicountDoctorDentist/discountDentist.aspx");

        }
    }
}
